//分页渲染
xuanRan();
function xuanRan(xuanRan_page) {
  $.ajax({
    type: "post",
    url: "http://118.195.129.130:3000/food/getInfoByPage",
    data: {
      page: xuanRan_page,
      per_page: 8,
    },
    success: function (data) {
      if (data.err === 0) {
        for (let i = 0; i < data.data.length; i++) {
          var tbody = document.querySelector("tbody");
          let tr = document.createElement("tr");
          tbody.append(tr);
          tr.className = "rendering_tr";
          tr.innerHTML = `
                     <td class="rendering_td_id" style="display:none">${data.data[i]._id}</td>
                     <td class="rendering_td_name">${data.data[i].name}</td>
                     <td class="rendering_td_price">${data.data[i].price}</td>
                     <td class="rendering_td_desc">${data.data[i].desc}</td>
                     <td class="rendering_td_typename">${data.data[i].typename}</td>
                     <td class="rendering_td_typeid">${data.data[i].typeid}</td>
                     <td>
                     <a href="javascript:;" class="btn_delect" onclick="delect_data('${data.data[i]._id}');"> 删除 
                     </a>
                     <a href="javascript:;" class="btn_change" onclick="change();">修改</a>
                     </td>`;
        }
      } else {
        alert("渲染失败！");
      }
    },
    error: function (err) {
      console.log(err);
    },
  });
}
//添加功能
var add = document.querySelector(".add");
var hidden_add = document.querySelector(".hidden_add");
var tbody = document.querySelector("tbody"); //获取tbody
var hidden_add_textArea_one_right = document.querySelector(
  ".hidden_add_textArea_one_right"
);
var hidden_add_textArea_two_right = document.querySelector(
  ".hidden_add_textArea_two_right"
);
var hidden_add_textArea_three_right = document.querySelector(
  ".hidden_add_textArea_three_right"
);
var hidden_add_textArea_four_right = document.querySelector(
  ".hidden_add_textArea_four_right"
);
var hidden_add_textArea_five_right = document.querySelector(
  ".hidden_add_textArea_five_right"
);
var hidden_add_btn = document.querySelector(".hidden_add_btn");
var hidden_add_delect = document.querySelector(".hidden_add_delect");
//添加功能时数据不全时提示，以及全的时候实现的添加功能
add.onclick = function () {
  hidden_add.style.display = "block";
  hidden_add_delect.onclick = function () {
    hidden_add.style.display = "none";
    hidden_add_textArea_one_right.value = "";
    hidden_add_textArea_two_right.value = "";
    hidden_add_textArea_three_right.value = "";
    hidden_add_textArea_four_right.value = "";
    hidden_add_textArea_five_right.value = "";
  };
  hidden_add_btn.onclick = function () {
    if (
      hidden_add_textArea_one_right.value == "" ||
      hidden_add_textArea_two_right.value == "" ||
      hidden_add_textArea_three_right.value == "" ||
      hidden_add_textArea_four_right.value == "" ||
      hidden_add_textArea_five_right.value == ""
    ) {
      alert("您输入的数据不全！");
    } else {
      $.ajax({
        type: "post",
        url: "http://118.195.129.130:3000/food/add",
        data: {
          name: $(".hidden_add_textArea_one_right").val(),
          price: $(".hidden_add_textArea_two_right").val(),
          desc: $(".hidden_add_textArea_three_right").val(),
          typename: $(".hidden_add_textArea_four_right").val(),
          typeid: $(".hidden_add_textArea_five_right").val(),
        },
        success: function (data) {
          if (data.err === 0) {
            alert("添加成功！");
            var tbody = document.querySelector("tbody");
            tbody.innerHTML = "";
            xuanRan();
            btn_delect.onclick = function () {
              hidden_add.style.display = "none";
            };
          } else {
            alert("添加失败！");
          }
        },
        error: function (err) {
          console.log(err);
        },
      });
      hidden_add_textArea_one_right.value = "";
      hidden_add_textArea_two_right.value = "";
      hidden_add_textArea_three_right.value = "";
      hidden_add_textArea_four_right.value = "";
      hidden_add_textArea_five_right.value = "";
    }
  };
};

//查询功能
var title_select_three_right = document.querySelector(
  ".title_select_three_right"
);
var office_btn = document.querySelector(".office_btn");
var table = document.querySelector(".table");
var table_one = document.querySelector(".table_one");
var tbody_one = document.querySelector(".tbody_one");
office_btn.onclick = function () {
  if (title_select_three_right.value == "") {
    alert("a");
  } else {
    table.style.display = "none";
    table_one.style.display = "block";
    xuanRan_one();
    function xuanRan_one() {
      $.ajax({
        type: "post",
        url: "http://118.195.129.130:3000/food/getInfoByKw",
        data: {
          kw: title_select_three_right.value,
        },
        success: function (data) {
          console.log(data);
          if (data.data.length != 0) {
            var tbody_one = document.querySelector(".tbody_one");
            for (let i = 0; i < data.data.length; i++) {
              let tr = document.createElement("tr");
              tbody_one.append(tr);
              tr.className = "rendering_tr";
              tr.innerHTML = `
                           <td class="rendering_td_id" style="display:none">${data.data[i]._id}</td>
                           <td class="rendering_td_name">${data.data[i].name}</td>
                           <td class="rendering_td_price">${data.data[i].price}</td>
                           <td class="rendering_td_desc">${data.data[i].desc}</td>
                           <td class="rendering_td_typename">${data.data[i].typename}</td>
                           <td class="rendering_td_typeid">${data.data[i].typeid}</td>
                           <td>
                           <a href="javascript:;" class="btn_delect" onclick="delect_data('${data.data[i]._id}');">删除</a>
                           <a href="javascript:;" class="btn_change" onclick="change();">修改</a>
                           </td>
                         `;
            }
          } else {
            var tbody_one = document.querySelector(".tbody_one");
            alert("未找到数据！");
            tbody_one.innerHTML = "";
            xuanRan();
          }
        },
        error: function () {
          alert("失败！");
          console.log(data.err);
        },
      });
    }
  }
};
// 删除操作
var tbody = document.querySelector("tbody");
var btn_delect = document.getElementsByClassName("btn_delect");
var rendering_tr = document.querySelectorAll(".rendering_tr");
var rendering_td_name = document.querySelectorAll(".rendering_td_name");
var rendering_td_price = document.querySelectorAll(".rendering_td_price");
var rendering_td_desc = document.querySelectorAll(".rendering_td_desc");
var rendering_td_typename = document.querySelectorAll(".rendering_td_typename");
var rendering_td_typeid = document.querySelectorAll(".rendering_td_typeid");
var rendering_td_id = document.querySelectorAll(".rendering_td_id");
function delect_data(id) {
  var flag = confirm("确认删除吗？");
  if (flag) {
    $.ajax({
      type: "post",
      url: "http://118.195.129.130:3000/food/del",
      data: {
        _id: id,
      },
      success: function (data) {
        if (data.err === 0) {
          console.log(data);
          tbody.innerHTML = "";
          tbody_one.innerHTML = "";
          xuanRan();
        } else {
          alert("删除失败！");
        }
      },
      error: function (err) {
        console.log(err);
      },
    });
  }
}
// 修改操作
//点击修改按钮后，对应的td里边添加可修改的属性，然后修改完成之后，用失去焦点事件，发送请求，最后刷新一下页面
var id_table = document.getElementById("id_table");
var change_a = id_table.getElementsByClassName("btn_change");
var hidden_revise = document.querySelector(".hidden_revise");
var hidden_revise_btn = document.querySelector(".hidden_revise_btn");
var hidden_revise_delect = document.querySelector(".hidden_revise_delect");
var hidden_revise_textArea_one_right = document.querySelector(
  ".hidden_revise_textArea_one_right"
);
var hidden_revise_textArea_two_right = document.querySelector(
  ".hidden_revise_textArea_two_right"
);
var hidden_revise_textArea_three_right = document.querySelector(
  ".hidden_revise_textArea_three_right"
);
var hidden_revise_textArea_four_right = document.querySelector(
  ".hidden_revise_textArea_four_right"
);
var hidden_revise_textArea_five_right = document.querySelector(
  ".hidden_revise_textArea_five_right"
);
//修改操作
function change() {
  hidden_revise.style.display = "block";
  hidden_revise_delect.onclick = function () {
    hidden_revise.style.display = "none";
  };
  let change_a = document.querySelectorAll(".btn_change");
  console.log(change_a.length);
  for (let i = 0; i < change_a.length; i++) {
    console.log(change_a.length);
    change_a[i].onclick = function () {
      alert("a");
      tbody.innerHTML = "";
      hidden_revise.style.display = "block";
      hidden_revise_textArea_one_right.value =
        change_a[i].parentNode.parentNode.children[1].innerHTML;
      hidden_revise_textArea_two_right.value =
        change_a[i].parentNode.parentNode.children[2].innerHTML;
      hidden_revise_textArea_three_right.value =
        change_a[i].parentNode.parentNode.children[3].innerHTML;
      hidden_revise_textArea_four_right.value =
        change_a[i].parentNode.parentNode.children[4].innerHTML;
      hidden_revise_textArea_five_right.value =
        change_a[i].parentNode.parentNode.children[5].innerHTML;
      hidden_revise_btn.onclick = function () {
        alert(change_a[i].parentNode.parentNode.children[0].innerHTML);
        $.ajax({
          type: "post",
          url: "http://118.195.129.130:3000/food/update",
          data: {
            name: hidden_revise_textArea_one_right.value,
            price: hidden_revise_textArea_two_right.value,
            desc: hidden_revise_textArea_three_right.value,
            typename: hidden_revise_textArea_four_right.value,
            typeid: hidden_revise_textArea_five_right.value,
            _id: change_a[i].parentNode.parentNode.children[0].innerHTML,
          },
          success: function (data) {
            if (data.err === 0) {
              console.log(data);
              alert("修改成功!");
              xuanRan();
            } else {
              alert("修改失败！");
            }
          },
          error: function (err) {
            console.log(err);
          },
        });
        tbody.innerHTML = "";
        tbody_one.innerHTML = "";
        // console.log(tr.length);
        let tr = document.createElement("tr");
        tbody_one.append(tr);
        tr.className = "rendering_tr";
        tr.innerHTML = `
                       <td class="rendering_td_id" style="display:none">${change_a[i].parentNode.parentNode.children[0].innerHTML}</td>
                       <td class="rendering_td_name">${hidden_revise_textArea_one_right.value}</td>
                       <td class="rendering_td_price">${hidden_revise_textArea_two_right.value}</td>
                       <td class="rendering_td_desc">${hidden_revise_textArea_three_right.value}</td>
                       <td class="rendering_td_typename">${hidden_revise_textArea_four_right.value}</td>
                       <td class="rendering_td_typeid">${hidden_revise_textArea_five_right.value}</td>
                       <td>
                       <a href="javascript:;" class="btn_delect" onclick="delect_data('${change_a[i].parentNode.parentNode.children[0].innerHTML}');">删除</a>
                       <a href="javascript:;" class="btn_change" onclick="change();">修改</a>
                       </td>
                     `;
      };
      xuanRan();
    };
  }
}
//重置
var title_right_again = document.querySelector(".title_right_again");
title_right_again.onclick = function () {
  location.reload();
};
//重新登录
var gongNeng_title_return = document.querySelector(".gongNeng_title_return");
gongNeng_title_return.onclick = function () {
  var flag_one = confirm("确认退出登录么吗？");
  if (flag_one) {
    window.location.href = "../html/login.html";
  }
  // 在原来的窗体直接跳转　window.location.href="你所要跳转的页面";
  //在新窗体打开页面　window.open('你所要跳转的页面');
};
//分页渲染
var gongNeng_fenYe = document.querySelector(".gongNeng_fenYe");
var gongNeng_fenYe_previous = document.querySelector(
  ".gongNeng_fenYe_previous"
);
var gongNeng_fenYe_number = document.querySelector(".gongNeng_fenYe_number");
var gongNeng_fenYe_next = document.querySelector(".gongNeng_fenYe_next");
var gongNeng_fenYe_inputNumber = document.querySelector(
  ".gongNeng_fenYe_inputNumber"
);
var gongNeng_fenYe_button = document.querySelector(".gongNeng_fenYe_button");
var hidden_tip_one = document.querySelector(".hidden_tip_one");
var hidden_tip_two = document.querySelector(".hidden_tip_two");
var hidden_tip_one_delect = document.querySelector(".hidden_tip_one_delect");
var hidden_tip_two_delect = document.querySelector(".hidden_tip_two_delect");
//渲染的  下一页
gongNeng_fenYe_next.onclick = function () {
  if (gongNeng_fenYe_number.innerHTML < 3) {
    gongNeng_fenYe_number.innerHTML++;
    tbody.innerHTML = "";
    xuanRan(gongNeng_fenYe_number.innerHTML);
  } else {
    gongNeng_fenYe_number.innerHTML == 3;
    hidden_tip_two.style.display = "block";
    hidden_tip_two_delect.onclick = function () {
      hidden_tip_two.style.display = "none";
    };
  }
};
//渲染的  上一页
gongNeng_fenYe_previous.onclick = function () {
  if (gongNeng_fenYe_number.innerHTML <= 1) {
    gongNeng_fenYe_number.innerHTML == 1;
    hidden_tip_one.style.display = "block";
    hidden_tip_one_delect.onclick = function () {
      hidden_tip_one.style.display = "none";
    };
  } else if (gongNeng_fenYe_number.innerHTML < 4) {
    gongNeng_fenYe_number.innerHTML--;
    tbody.innerHTML = "";
    xuanRan(gongNeng_fenYe_number.innerHTML);
  }
};
//渲染的  输入的页数
var gongNeng_fenYe_inputNumber = document.querySelector(
  ".gongNeng_fenYe_inputNumber"
);
var gongNeng_fenYe_button = document.querySelector(".gongNeng_fenYe_button");
gongNeng_fenYe_button.onclick = function () {
  console.log(gongNeng_fenYe_inputNumber.value);
  if (
    gongNeng_fenYe_inputNumber.value >= 1 &&
    gongNeng_fenYe_inputNumber.value <= 3
  ) {
    tbody.innerHTML = "";
    xuanRan(gongNeng_fenYe_inputNumber.value);
  } else hidden_tip_two.style.display = "block";
  hidden_tip_two_delect.onclick = function () {
    hidden_tip_two.style.display = "none";
  };
};

//订单管理
var all_one_div = document.querySelector(".all_one_div");
var all_two_div = document.querySelector(".all_two_div");
var left_check_two = document.querySelector(".left_check_two");
var left_check_one_two = document.querySelector(".left_check_one_two");
left_check_two.onclick = function () {
  all_one_div.style.display = "none";
  all_two_div.style.display = "block";
};
left_check_one_two.onclick = function () {
  all_one_div.style.display = "block";
  all_two_div.style.display = "block";
};
//返回登录
var gongNeng_title_return_two = document.querySelector(
  ".gongNeng_title_return_two"
);
gongNeng_title_return_two.onclick = function () {
  var flag_four = confirm("确认退出登录么吗？");
  if (flag_four) {
    window.location.href = "../html/login.html";
  }
};
//渲染一下订单页面

// xuanRan_dingDan();
// function xuanRan_dingDan() {
//   $.ajax({
//     type: "post",
//     url: "http://118.195.129.130:3000/order/getInfoByPage_order",
//     data: {
//       page: 1,
//       per_page:5,
//     },
//     success: function (data) {
//       if (data.err === 0) {
//         for (let i = 0; i < data.data.length; i++) {
//           var tbody = document.querySelector("tbody");
//           let tr = document.createElement("tr");
//           tbody.append(tr);
//           tr.className = "rendering_tr_dingDan";
//           tr.innerHTML = `
//                      <td class="rendering_dingDan_td_id" style="display:none">${data.data[i]._id}</td>
//                      <td class="rendering_dingDan_td_us">${data.data[i].us}</td>
//                      <td class="rendering_dingDan_td_amount">${data.data[i].amount}</td>
//                      <td class="rendering_dingDan_td_phone">${data.data[i].phone}</td>
//                      <td class="rendering_dingDan_td_pay">${data.data[i].pay}</td>
//                      <td>
//                      <a href="javascript:;" class="btn_delect_two"> 删除 
//                      </a>
//                      <a href="javascript:;" class="btn_change_two">修改
//                      </a>
//                      </td>`;
//         }
//       } else {
//         alert("渲染失败！");
//       }
//     },
//     error: function (err) {
//       console.log(err);
//     },
//   });
// }
